<template>
  <div class="warehouse">
    <el-row class="head fff">
      <el-form
        :inline="true"
        :model="searchForm"
        label-position="top"
        class="search"
      >
        <el-col :span="6">
          <el-form-item label="类型编号">
            <el-input
              v-model="searchForm.like_code"
              placeholder="请输入"
            /> </el-form-item></el-col>
        <el-col :span="6">
          <el-form-item label="类型名称">
            <el-input
              v-model="searchForm.like_name"
              placeholder="请输入"
            /> </el-form-item></el-col>
        <el-col :span="6" style=" height:1px">
          <div />
        </el-col>
        <el-col :span="6" class="submit">
          <el-form-item>
            <el-button type="primary" @click="onSubmit">搜索</el-button>
            <el-button @click="reset">重置</el-button>
          </el-form-item></el-col>
      </el-form>
    </el-row>
    <el-row class="table fff">
      <el-row class="add">
        <el-button
          type="success"
          @click="add(true)"
        >新增货品类型</el-button>
      </el-row>
      <div v-if="isNowData" class="now-data">
        <span class="imgIcon" /><span style="color: #b5abab;">暂无数据</span>
      </div>
      <template v-else>
        <el-table
          :data="tableData.records"
          stripe
          border
          :row-style="{}"
          :header-cell-style="{
            background: '#f9f6ee',
            borderColor: '#f5efee!important',
            padding: '10px 0',
          }"
          :cell-style="{ borderColor: '#f5efee!important', padding: '10px 0' }"
        >
          <el-table-column label="序号" width="68" align="center">
            <template v-slot="{ $index }">{{ $index + 1 }}</template>
          </el-table-column>
          <el-table-column
            prop="code"
            label="货品类型编号"
            align="center"
          />
          <el-table-column
            prop="name"
            label="货品类型名称"
            align="center"
          />
          <el-table-column
            prop="updateTime"
            label="更新时间"
            align="center"
            width="200"
          />
          <el-table-column
            label="操作"
            align="center"
          >
            <template v-slot="{ row }">
              <span
                class="operate"
                @click="add(false, row)"
              >编辑</span>
              <span class="operate" @click="del(row)">删除</span>
            </template>
          </el-table-column>
        </el-table>
        <PageTools
          :data="tableData"
          @handleSizeChange="handleSizeChangeFn"
          @handleCurrentChange="handleCurrentChangeFn"
        />
      </template>
    </el-row>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form
        ref="addForm"
        :rules="rules"
        :model="addForm"
        label-position="top"
        class="search"
      >
        <el-form-item label="类型编号" prop="code">
          <el-input
            v-model="addForm.code"
            disabled
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="类型名称" prop="name">
          <el-input
            v-model="addForm.name"
            placeholder="请输入"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { nextCodeFactory } from '@/api/codeFactory'
import { getGoodsType, addGoodsType, setGoodsType, delGoodsType } from '@/api/goodsType'
export default {
  data() {
    return {
      isAdd: true,
      dialogVisible: false,
      isNowData: true,
      searchForm: {
        current: 1,
        size: 10,
        like_code: null,
        like_name: null
      },
      addForm: {
        code: null,
        name: null,
        id: null
      },
      rules: {
        code: {
          required: true,
          message: '请输入',
          trigger: 'blur'
        },
        name: {
          required: true,
          message: '请输入',
          trigger: 'blur'
        }
      },
      tableData: {}
    }
  },
  watch: {
    dialogVisible(val) {
      if (!val) {
        this.addForm = {
          code: null,
          name: null,
          id: null
        }
      }
    }
  },
  created() {
    this.getGoodsType()
  },
  methods: {
    async nextCodeFactory() {
      const { data } = await nextCodeFactory('HPLX')
      this.addForm.code = data
    },
    async getGoodsType() {
      const { data } = await getGoodsType(this.searchForm)
      this.isNowData = !data.records.length
      this.tableData = data
    },
    add(flag, row) {
      if (!flag) {
        this.isAdd = flag
        this.addForm.code = row.code
        this.addForm.name = row.name
        this.addForm.id = row.id
      } else {
        this.addForm.id = null
        this.nextCodeFactory()
      }
      this.dialogVisible = true
    },
    addSubmit() {
      this.$refs.addForm.validate(async(valid) => {
        if (valid) {
          if (this.isAdd) {
            await addGoodsType(this.addForm)
            this.$message.success('新增成功')
          } else {
            await setGoodsType(this.addForm)
            this.$message.success('修改成功')
          }
          this.dialogVisible = false
          this.searchForm.current = 1
          this.getGoodsType()
        } else {
          this.$message.warning('请完善表单')
          return false
        }
      })
    },
    async del(data) {
      this.$confirm(`确定要删除: ${data.name} 吗？`, '确认删除').then(
        async(_) => {
          await delGoodsType({ 'ids[]': data.id })
          this.getGoodsType()
          this.$message.success('删除成功')
        }
      )
    },
    onSubmit() {
      this.searchForm.current = 1
      this.getGoodsType()
    },
    reset() {
      this.searchForm.like_code = null
      this.searchForm.like_name = null
      this.searchForm.status = null
      this.onSubmit()
    },
    handleSizeChangeFn(val) {
      this.searchForm.size = val
      this.searchForm.current = 1
      this.getGoodsType()
    },
    handleCurrentChangeFn(val) {
      this.searchForm.current = val
      this.getGoodsType()
    }
  }
}
</script>

<style lang="scss" scoped>
.warehouse {
  .table {
    padding: 0;
    margin-top: 20px;
    .add {
      padding: 20px 30px;
      border-bottom: 1px solid #f5efee;
    }
    .operate {
      color: #ffb200;
      cursor: pointer;
      margin: 0 10px;
      &:hover {
        color: #ff8e00;
      }
    }
  }
  .el-dialog__wrapper{
    .el-form{
      margin-left: 25px;
    }
  }
}
</style>
